package ph.com.gs3.formalistics.view.document.specialfields;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.vo.business.form.FormFieldData;
import ph.com.gs3.formalistics.view.document.abstractfields.AbstractFieldView;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class DynamicImageField extends AbstractFieldView {

	public static final String TAG = DynamicImageField.class.getSimpleName();

	private ImageView ivDynamicImage;
	private DynamicImageFieldActionListener actionListener;

	public DynamicImageField(Context context, FormFieldData formField) {
		super(context, formField.getId());

		createView();
	}

	protected void createView() {
		LayoutInflater.from(getContext()).inflate(R.layout.field_dynamic_image, this, true);

		ivDynamicImage = (ImageView) findViewById(R.id.DynamicImage_ivDynamicImage);
		ivDynamicImage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (actionListener != null) {
					actionListener.onSelectImage(DynamicImageField.this);
				}
			}
		});
	}

	@Override
	protected void initializeChangeListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public String getValue() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setValue(String value) {
		// TODO Auto-generated method stub

	}

	public void setImagePath(String filePath) {
		Bitmap bitmap = BitmapFactory.decodeFile(filePath);
		setImage(bitmap);
	}

	public void setImage(Bitmap bitmap) {

		int bitmapWidth = bitmap.getWidth();
		Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();

		ivDynamicImage.setImageBitmap(bitmap);

		if (display.getWidth() > bitmapWidth) {
			FLLogger.d(TAG, "display width: " + bitmapWidth);
			ivDynamicImage.getLayoutParams().width = LayoutParams.WRAP_CONTENT;
		} else {
			FLLogger.d(TAG, "match parent");
			ivDynamicImage.getLayoutParams().width = LayoutParams.MATCH_PARENT;			
		}
		

	}

	public DynamicImageFieldActionListener getDynamicImageFieldActionListener() {
		return actionListener;
	}

	public void setDynamicImageFieldActionListener(DynamicImageFieldActionListener actionListener) {
		this.actionListener = actionListener;
	}

	public static interface DynamicImageFieldActionListener {
		public void onSelectImage(DynamicImageField source);
	}

	@Override
	public void showError(String errorMessage) {
		FLLogger.e(TAG, "showError not yet supported");
	}

}

